/*V.09040202/A.Gavin Ke/D.qbuy index*/
var phonepay = {
	src:function(item){ 
		var sValue=location.search.match(new RegExp("[\?\&]"+item+"=([^\&]*)(\&?)","i"));
		if(sValue){
			return sValue?sValue[1]:sValue; 
		}else{
			return 'newhomepage';
		}	
	},
	retrieve:function(fun){
		if($('phonenumber').getA('value').length == 0){
			$("paymoney").html('请输入手机号码','strong');
			$('qbuyadv').setS('display','none');
			$('qbuyadj').setS('display','none');
		}else if($('phonenumber').getA('value').length == 11){
			JEND.DSS.get('http://cz.eachnet.com/eachcoupon/section.do',fun,{type:'json',mphone:$('phonenumber').getA('value'),amount:$('phonemoney').getA('value'),time_stamp:(new Date()).getTime()});
		}else{
			$("paymoney").html('手机号码应为11位数字','strong');
			$('qbuyadv').setS('display','none');
			$('qbuyadj').setS('display','none');
		}
	},
	getSection:function(obj){
		if (obj.response.result == 'false'){
			if (obj.response.redirect == 'false'){
				var msg = obj.response.msg;
				$("paymoney").html(decodeURI(msg),'strong');
				$('qbuyadv').setS('display','none');
				$('qbuyadj').setS('display','none');
			}else if(obj.response.redirect == 'true'){
				$("paymoney").html('系统暂时缺货中','strong');
				$('payphone').setS('display','none');
				$('qbuyadv').setS('display','none');
				$('qbuyadj').setS('display','block');
			}
		}else if(obj.response.result == 'true'){
			var favamount = obj.response.favamount;
			$("paymoney").html('支付金额:<em>'+favamount+'</em>元');
			$('payphone').setS('display','block');
			$('qbuyadv').setS('display','none');
			$('qbuyadj').setS('display','none');
		}
	},
	goSection:function(obj){
		if (obj.response.result == 'false'){
			$("paymoney").html(decodeURI(obj.response.msg),'strong');
		}else if(obj.response.result == 'true'){
			$("inputform").getA('mphone').value = $('phonenumber').getA('value');
			$("inputform").getA('productNum').value = obj.response.productnum;
			$("inputform").getA('amount').value = $('phonemoney').getA('value');
			$("inputform").getA('signature').value = obj.response.signature;
			$("inputform").setA('action','http://cz.eachnet.com/eachbatch/unreg.do?merSrc=' + phonepay.src('src'));
			$("inputform")[0].submit();
		}
	},
	pay:function(){
		$('phonenumber').on('keypress',function(){phonepay.retrieve(phonepay.getSection);});
		$('phonenumber').on('blur',function(){phonepay.retrieve(phonepay.getSection);});
		$('phonemoney').on('change',function(){phonepay.retrieve(phonepay.getSection);});
		$('payphone').on('click',function(){phonepay.retrieve(phonepay.goSection);});
	}
};
document.onReady(function(){
	phonepay.pay();
});